package com.sky.Protocol;

import io.netty.buffer.ByteBuf;

/**
 * Created by sun on 2017-06-11.
 */
public class MessageProtocol {

    /**
     * body 长度
     */
    private int bodyLen;

    /**
     * 协议包头
     */
    private MessageHeader header;

    /**
     * body内容
     */
    private String body;

    public MessageProtocol() {

    }

    public MessageProtocol(String body) {
        this.header = new MessageHeader(1,0,0,0);
        this.body = body;
    }

    public MessageProtocol(int version,int msgType,int tag,int packId,String body) {
        this.header = new MessageHeader(version,msgType,tag,packId);
        this.body = body;
    }

    public MessageProtocol(int bodyLen,byte[] headerBytes,byte[] bodyBytes) {
        this.header = new MessageHeader(headerBytes);
        this.body = new String(bodyBytes);
    }

    public int getBodyLen() {
        return bodyLen;
    }

    public void setBodyLen(int bodyLen) {
        this.bodyLen = bodyLen;
    }

    public MessageHeader getHeader() {
        return header;
    }

    public void setHeader(MessageHeader header) {
        this.header = header;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

    public void msg2ByteBuf(ByteBuf byteBuf) {
        bodyLen = body.getBytes().length + MessageHeader.HEADER_LENGTH;
        byteBuf.writeInt(bodyLen);
        header.msgHeader2ByteBuf(byteBuf);
        byteBuf.writeBytes(body.getBytes());
    }

}
